/**
 * Copyright &copy; 2016 青岛数码人科技有限公司   All rights reserved.
 */
package cn.net.ecode.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.modules.cms.entity.ArticleData;
import cn.net.ecode.modules.cms.service.ArticleDataService;

/**
 * 文章详情Controller
 * @author 车磊岩
 * @version 2017-04-04
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/articleData")
public class ArticleDataController extends BaseController {

	@Autowired
	private ArticleDataService articleDataService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ArticleData get(String id, boolean isNewRecord) {
		return articleDataService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("cms:articleData:view")
	@RequestMapping(value = {"list", ""})
	public String list(ArticleData articleData, Model model) {
		model.addAttribute("articleData", articleData);
		return "modules/cms/articleDataList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("cms:articleData:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<ArticleData> listData(ArticleData articleData, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ArticleData> page = articleDataService.findPage(new Page<ArticleData>(request, response), articleData); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("cms:articleData:view")
	@RequestMapping(value = "form")
	public String form(ArticleData articleData, Model model) {
		model.addAttribute("articleData", articleData);
		return "modules/cms/articleDataForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("cms:articleData:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(ArticleData articleData) {
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, articleData)){
			return renderResult(Global.FALSE, message);
		}
		articleDataService.save(articleData);
		return renderResult(Global.TRUE, "保存文章详情成功！");
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("cms:articleData:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(ArticleData articleData) {
		articleDataService.delete(articleData);
		return renderResult(Global.TRUE, "删除文章详情成功！");
	}

}